package zf.display.tools
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import zf.data.tools.Memory;
	
	/**
	 * 加载完成事件 
	 * @eventType flash.events.Event.COMPLETE
	 */
	[Bindable]
	[Event(name="complete", type="flash.events.Event")]

	/**
	 * 显示对象加载序列。加载外部PNG,JPG,SWF
	 * @author zhangfeng
	 * @langversion 3.0 
	 * @playerversion Flash 10 
	 */
	public class DisplayLoaderArray extends EventDispatcher
	{
		private var urlList:Vector.<String>;
		private var contentList:Array;
		
		private var _index:int=-1;
		private var loaderList:Vector.<Loader>;
		public function DisplayLoaderArray(list:Vector.<String>=null)
		{
			if(list)
			{
				urlList=list;
			}else
			{
				urlList=new Vector.<String>();
			}
			contentList=[];
			loaderList=new Vector.<Loader>();
		}
		/**
		 * 添加一个加载 
		 * @param url 加载地址
		 * 
		 */
		public function push(url:String):void
		{
			urlList.push(url);
		}
		/**
		 * 获得内容集的副本 
		 * @return 
		 * 
		 */
		public function get contents():Array
		{
			return contentList.concat();
		}
		/**
		 * 获得第id的内容 
		 * @param id
		 * @return 
		 * 
		 */
		public function getContent(id:uint):DisplayObject
		{
			return contentList[id];
		}
		/**
		 * 开始加载 
		 * 
		 */
		public function start():void
		{
			_index=0;
			
			loadURL(urlList[_index]);
		}
		/**
		 * 是否已经加载 
		 * @param id
		 * @return 
		 * 
		 */
		public function isLoad(id:uint):Boolean
		{
			if(contentList.length<=id)
			{
				return false;
			}
			return contentList[id]==null;
		}
		
		private function loadURL(url:String):void
		{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT,onInitHandler);
			loader.contentLoaderInfo.addEventListener("io_error",onErrorHandler);
			loader.load(new URLRequest(url));
			loaderList.push(loader);
		}
		private function onInitHandler(e:Event):void
		{
			var loaderInfo:LoaderInfo=LoaderInfo(e.currentTarget);
			var loader:Loader=loaderInfo.loader;
			loader.contentLoaderInfo.removeEventListener(Event.INIT,onInitHandler);
			loader.contentLoaderInfo.removeEventListener("io_error",onErrorHandler);
			contentList[_index]=loader.content;
			loader.unload();
			_index++;
			if(_index<length)
			{
				loadURL(urlList[_index]);				
			}else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		private function onErrorHandler(e:Event):void
		{
			var loaderInfo:LoaderInfo=LoaderInfo(e.currentTarget);
			var loader:Loader=loaderInfo.loader;
			loader.contentLoaderInfo.removeEventListener(Event.INIT,onInitHandler);
			loader.contentLoaderInfo.removeEventListener("io_error",onErrorHandler);
			contentList[_index]=null;
			loader.unload();
			_index++;
			if(_index<length)
			{
				loadURL(urlList[_index]);			
			}else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		/**
		 * 长度 
		 * @return 
		 * 
		 */
		public function get length():uint
		{
			return urlList.length;
		}
		
		/**
		 * 正在加载的序号,-1表示还没有开始加载
		 * @return 
		 * 
		 */
		public function get index():int
		{
			return _index;
		}
		/**
		 * 破坏并清空DisplayLoaderArray的所有数据 
		 * @param gc 是否自动执行垃圾回收
		 * 
		 */
		public function dispose(gc:Boolean=true):void
		{
			for(var i:uint=0;i<loaderList.length;i++)
			{
				/*try
				{
					loaderList[i].unloadAndStop(false);
					continue;
				}
				catch(e:Error)
				{
					
				}*/
				try
				{
					loaderList[i].unload();
				}
				catch(e:Error)
				{
					
				}
			}
			var dp:DisplayObject
			/*for(i=0;i<contentList.length;i++)
			{
				try
				{
					dp=contentList[i];
					dp.parent.removeChild(dp);
				}
				catch(e:Error)
				{
					
				}
			}*/
			loaderList=new Vector.<Loader>();
			contentList=[];
			urlList=new Vector.<String>();
			if(gc)
			{
				Memory.gc();
			}
		}
	}
}